home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / vla / ctut2vla / calc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-23  |  6.4 KB  |  132 lines

  1. /*                     Calculator Addition Tutorial In C
  2.                                         By : Desolation/VLA
  3.  
  4.         This is a small calculator program that will add numbers for you under
  5.     a certain constraint (you can only add numbers that are from -99999 to
  6.     99999).  There is a small menu setup that is also included.  As the
  7.     tutorial continues, I will integrate more operations such as dividing,
  8.     multiplying, to the power of, averaging, and so on.  This will also allow
  9.     me to implement the menu further to show you how to incorporate one style
  10.     for a menu system also.  Please, comment on Phantasm.  I will be more
  11.     than happy to try and explain any part of the coding.
  12. */
  13.  
  14.  
  15. #include <stdio.h>      // Include standard header file.
  16.  
  17. void main  (void);      // Function protyping to setup for the functions
  18. void addit (void);      //  I will write later (not really necessary, but
  19.                                 //  good programming practice).
  20.  
  21. void main (void)        // The actual main function.
  22. {
  23.     char selection;      // Setup variable 'selection' which is of type char.
  24.  
  25. /* A 'do while' loop is used to repeat the menu system after every function
  26.     you do, such as adding, subtracting, etc. The exiting condition is
  27.     explained lower in the while section. */
  28.  
  29.     do {                                  // Start the do loop.
  30.         fflush(stdin);                     // flush keyboard buffer (don't worry about this yet).
  31.         printf("1.  Add Numbers \n");      // Print first option of menu.
  32.         printf("Q.  Quit Program \n");     // Print Termination option of menu.
  33.         printf("Enter Menu Selection : "); // Print text for menu selection.
  34.         selection = getchar();             // This grabs the char for the menu selection
  35.                                                       //  which is '1' or 'q'.
  36.  
  37. /* A 'switch' command is used for case statements.  This allows you to
  38.     constrain which selections the user can make, and tells the program which
  39.     function to goto for each selection.  You put 'selection' inside the ()
  40.     because that's the variable that will be changed by the user.  For example,
  41.     if the user were to  choose '1' for Add Numbers, the case statement which
  42.     corresponds to '1' tells it to goto the 'addit' function which is defined
  43.     later on in the code.  Break exits out of the switch block after the addit
  44.     function was completed.  If you didn't have this break statement, and the
  45.     user chose '1' to add number, then it would call the addit function routine,
  46.     do whatever the addit function said to do, come back to the switch statement
  47.     block, then continue on to the next statement.  The break tells it to
  48.     not contine on after the addit routine is called.  When the user inputs
  49.     'q', it just breaks out of the switch block, and then contines on with
  50.     the other commands after the switch statement (where it will say to
  51.     exit). */
  52.  
  53.         switch(selection) {  // Begin switch statement.
  54.             case '1' :        // If user put '1' then go to addit function.
  55.                 addit();       // Runs addit. () is used because no values get sent in.
  56.                 break;         // Break out of the switch block.
  57.             case 'q' :        // If user inputs 'q' then simply break out.
  58.                 break;
  59.         }                    // End switch block.
  60.  
  61. /* The 'while' is used for the looping structure.  When the switch block is
  62.     finished, a value is stuck in it (either '1' or 'q' for this program).
  63.     As long as selection is not equal to 'q', then keep DOing the code that's
  64.     between the 'do' up above and the 'while' below. */
  65.  
  66.         } while (selection != 'q');
  67.  
  68.     printf("Calculator Program Terminated.\n");  // Print termination notice.
  69. }                                               // End of the main program.
  70.  
  71. void addit (void)                               // Start of the addit function.
  72. {
  73.     int value1,value2,value3;                    // Setup 3 variables of type integer.
  74.  
  75. /* The PROMPTA is used as another looping structure.  This is called a
  76.     'goto loop'.  If you notice the 'if' statement a few lines below, it
  77.     puts a constraint or condition on the value the user enters in.  It has
  78.     to be between 9999 and -9999 (I only did this to show how to use an
  79.     'if' statement, no other reason).  What happens is that a number will
  80.     be stored into value1, then the number in value1 is checked.  If it's
  81.     greater than 9999 OR less then -9999 text comes up that says 'Value
  82.     not Valid'.  Then the 'goto PROMPTA' statement goes back up to the place
  83.     where 'PROMPTA:' (NOTICE a colon is used instead of a semi-colon!).
  84.     is located and executes the code from that spot over again.  REMEMBER!
  85.     C is case sensitive, 'PROMPTA' and 'prompta' are not the same.  It's good
  86.     programming practice to use capitals for goto loop names because it's easy
  87.     to keep track of. If value1 satisfies the conditions, then it goes on with
  88.     the code. */
  89.  
  90. PROMPTA:
  91.     printf("Enter In the 1st value  : ");  // Text to get 1st number.
  92.     scanf("%d",&value1);                   // Grab that # and store it into value1.
  93.     if (value1 > 9999 | value1 < -9999) {  // Checks value1.
  94.         printf("Value not valid.\n");       // If value1 > 9999 OR value1 < -9999
  95.         goto PROMPTA;                       //  then it goes up to PROMPTA and
  96.     }                                      //  re-executes this part of code again.
  97.  
  98. PROMPTB:                                  // PROMPTB is done the same as PROMPTA.
  99.     printf("Enter In the 2nd number : ");
  100.     scanf("%d",&value2);
  101.     if (value2 > 9999 | value2 < -9999) {
  102.         printf("Value not valid.\n");
  103.         goto PROMPTB;
  104.     }
  105.  
  106.     value3 = value1+value2;       // Store into value3, the addition of value1
  107.                                             //  and value2.
  108.  
  109.     printf("%d + %d = %d \n\n", value1,value2,value3); // Print value1, value2,
  110.                                                                         //  and the addition of
  111.                                                                         //  the 2 values.
  112.  
  113. }                                // End of Addit function
  114.  
  115.  
  116.  
  117. /* That sums it up for now.  If you have any questions of comments, leave
  118.     them on Phantasm (206)232-5912 (in case you forgot the number) or mail
  119.     me.  As soon as we get through this tutorial, the future progression will
  120.     be to add more commands to the menu system to see how to handle multiple
  121.     case statements, do the other numerical operators (subtraction, division,
  122.     etc.), do relational operators (less than, greater than, etc.) and
  123.     whatever else we can jam in here as time goes on and we all learn a
  124.     little more.  Thanks for learning.
  125.  
  126.     VLA email : vlasite@carson.u.washington.edu
  127.  
  128.                                                         Desolation/VLA
  129.                                                         decko@u.washington.edu
  130.                                                         Phantasm CoSysop
  131. */
  132.